package cn.com.generaldata.os.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;

public class InitialScriptUtil {

	private static final int INDEX_TYPE_NAME = 0;
	private static final int INDEX_TYPE_NO = 1;
	private static final int INDEX_TYPE_REPORT_ADMIN = 2;
	private static final int INDEX_TYPE_APPROVE_ADMIN = 3;
	private static final int INDEX_TYPE_APPROVE_NO = 4;
	private static final int INDEX_TYPE_EXPIRE_DATE = 5;
	private static final int INDEX_TYPE_MEASURE_UNIT = 6;

	private static final String INSERT_SQL = "INSERT INTO `tbl_report_type`"
			+ "(`id`,`type_name`,`type_no`,`type_code`,`type_report_admin`,`type_approve_admin`,"
			+ "`type_approve_no`,`type_expire_date`,`type_measure_unit`) VALUES"
			+ "(${id},${typeName},${typeNo},${typeCode},${typeReportAdmin},${typeApproveAdmin},"
			+ "${typeApproveNo},${typeExpireDate},${typeMeasureUnit});\n";

	public static Properties getReportTypeConfiguration(String path) throws FileNotFoundException, IOException {
		if (path == null || StringUtils.isBlank(path.trim())) {
			path = "report_types.properties";
		}

		ClassPathResource cpr = new ClassPathResource(path);
		Properties properties = new Properties();
		File file = new File(cpr.getPath());
		InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
		properties.load(reader);

		return properties;
	}

	private static String generateSource(Properties properties) {
		StringBuffer sb = new StringBuffer();
		for (Object key : properties.keySet()) {
			sb.append(replaceSQL(key.toString(), properties.getProperty(key.toString())));
		}

		return sb.toString();
	}

	private static void write2File(String source, String path) throws IOException {
		FileWriter writer = new FileWriter(path);
		writer.write(source, 0, source.length());
		writer.flush();
	}

	private static String replaceSQL(String propertyKey, String propertyValue) {

		String[] otherColumns = propertyValue.split("\\|");

		String typeCode = "\"" + propertyKey + "\"";
		String typeName = "\"" + otherColumns[INDEX_TYPE_NAME] + "\"";
		String typeNo = "\"" + otherColumns[INDEX_TYPE_NO] + "\"";
		String typeReportAdmin = "\"" + otherColumns[INDEX_TYPE_REPORT_ADMIN] + "\"";
		String typeApproveAdmin = "\"" + otherColumns[INDEX_TYPE_APPROVE_ADMIN] + "\"";
		String typeApproveNo = "\"" + otherColumns[INDEX_TYPE_APPROVE_NO] + "\"";
		String typeExpireDate = "\"" + otherColumns[INDEX_TYPE_EXPIRE_DATE] + "\"";
		String typeMeasureUnit = otherColumns.length == 7 ? "\"" + otherColumns[INDEX_TYPE_MEASURE_UNIT] + "\"" : null;

		return INSERT_SQL
				.replaceAll("\\$\\{id\\}",
						"\"" + String.valueOf(UUID.randomUUID().toString().replaceAll("-", "") + "\""))
				.replaceAll("\\$\\{typeName\\}", typeName).replaceAll("\\$\\{typeNo\\}", typeNo)
				.replaceAll("\\$\\{typeCode\\}", typeCode).replaceAll("\\$\\{typeReportAdmin\\}", typeReportAdmin)
				.replaceAll("\\$\\{typeApproveAdmin\\}", typeApproveAdmin)
				.replaceAll("\\$\\{typeApproveNo\\}", typeApproveNo)
				.replaceAll("\\$\\{typeExpireDate\\}", typeExpireDate)
				.replaceAll("\\$\\{typeMeasureUnit\\}", typeMeasureUnit == null ? "NULL" : typeMeasureUnit);
	}

	public static void main(String[] args) {
		Properties properties = null;
		try {
			properties = getReportTypeConfiguration("F:\\report_types.properties");
			String source = generateSource(properties);
			write2File(source, "F:\\report_types.sql");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
